﻿using System;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace InfoPathExtractionRules
{
    /// <summary>
    /// Extracts CurrentFormData from InfoPath requests.
    /// Substitutes the dynamic data extracted from an earlier request into the next InfoPath postback.
    /// </summary>
    public class ExtractAndSubstituteDynamicInfoPathData : WebTestPlugin
    {
        private string invocationPath = IPExtractionUtil.DefaultFormServerPath;
        /// <summary>
        /// String used to identify invocation and full page postback requests.
        /// If the InfoPath form is in an XmlFormViewControl, then this property should be used.
        /// </summary>
        public string InvocationPath
        {
            get
            {
                return invocationPath;
            }

            set
            {
                if (string.IsNullOrEmpty(value))
                    invocationPath = IPExtractionUtil.DefaultFormServerPath;
                else
                    invocationPath = value;
            }
        }

        /// <summary>
        /// Substitutes the dynamic data extracted from an earlier request into the next InfoPath postback.
        /// </summary>
        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
            base.PreRequest(sender, e);

            if (IPExtractionUtil.IsPartialPostback(e.Request))
            {
                StringHttpBody body = (StringHttpBody)e.Request.Body;
                body.BodyString = IPExtractionUtil.SubstituteCurrentFormData(body.BodyString, e.WebTest.Context);
            }
            else if (IPExtractionUtil.IsFullPagePostback(e.Request, invocationPath))
            {
                FormPostHttpBody postBackBody = (FormPostHttpBody)e.Request.Body;
                foreach (FormPostParameter parameter in postBackBody.FormPostParameters)
                {
                    if (parameter.Name == IPExtractionUtil.EventLogFormPostbackParameterName)
                    {
                        parameter.Value = IPExtractionUtil.SubstituteCurrentFormData(parameter.Value, e.WebTest.Context);
                    }
                }
            }
        }

        /// <summary>
        /// Extracts CurrentFormData from InfoPath requests.
        /// </summary>
        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
            base.PostRequest(sender, e);

            if( e.ResponseExists &&
                e.Response.StatusCode == System.Net.HttpStatusCode.OK &&
                  ( IPExtractionUtil.IsPartialPostback(e.Request) ||
                    IPExtractionUtil.IsInvocation(e.Request, invocationPath) ||
                    IPExtractionUtil.IsFullPagePostback(e.Request, invocationPath)
                   )
               )
            {
                string var = IPExtractionUtil.getg_ObjCurrentFormData(e.Response.BodyString);
                CurrentFormData data = new CurrentFormData(var);
                e.WebTest.Context[CurrentFormData.ContextParameterName] = data;
            }
        }
    }
}
